package com.meepo.feed

import com.meepo.basic.schema.common.PageResult
import com.meepo.basic.schema.feed.Item
import com.meepo.basic.schema.common.Pagination
import com.meepo.basic.schema.common.ResponseWrapper
import com.meepo.sdk.utils.SPUtils

object Func {

    private const val STORE_SEARCH_KEY = "searchHistoryKey"
    private const val STORE_READ_HISTORY_KEY = "readHistoryKey"

    fun saveReadHistory(item: Item) {
        SPUtils.getInstance().upsertObjectSet(STORE_READ_HISTORY_KEY, item, Item::class.java)
    }

    fun getReadHistory(): PageResult<Item>? {
        val list =
            SPUtils.getInstance().getObjectSet(STORE_READ_HISTORY_KEY, Item::class.java)?.reversed()
                ?.toList()

        var total = 0
        list?.let { total = list.size }

        return PageResult(
            key = "",
            pagination = Pagination(pageSize = 1, current = 1, total = total),
            items = list
        )
    }

    fun getReadHistoryV2(): ResponseWrapper<PageResult<Item>> {
        val list =
            SPUtils.getInstance().getObjectSet(STORE_READ_HISTORY_KEY, Item::class.java)?.reversed()
                ?.toList()

        var total = 0
        list?.let { total = list.size }

        return ResponseWrapper(
            code = 200,
            message = "",
            data = PageResult(
                key = "",
                pagination = Pagination(pageSize = 1, current = 1, total = total),
                items = list
            )
        )
    }

    fun fetchSearchHistory(): List<String> {
        return SPUtils.getInstance()
            .getStringSet(STORE_SEARCH_KEY)?.toMutableList()?.reversed() ?: arrayListOf()
    }

    fun putSearchHistory(word: String) {
        SPUtils.getInstance()
            .appendStringSet(STORE_SEARCH_KEY, word)
    }

    fun clearSearchHistory() {
        SPUtils.getInstance().remove(STORE_SEARCH_KEY)
    }

}